use std::{env, ffi::CString};

use player::{CustomError, MediaPlayer};

//https://www.cnblogs.com/breakpointlab/p/13283187.html
#[tokio::main]
async fn main() -> Result<(), CustomError> {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 {
        println!("Please enter the video file address. eg: /home/xxx/data/xxx.mp4");
        return Err(CustomError::SomeError);
    }

    let file_path = args.get(1).unwrap();
    //println!("file_path: {}", file_path);

    let mut media_player = MediaPlayer::new(
        CString::new(file_path.to_owned()).unwrap(),
        get_title(&file_path).unwrap().to_owned(),
    );
    media_player.player().await?;

    return Ok(());
}

pub fn get_title(path: &str) -> Option<&str> {
    if let Some(sep) = path.rfind('/') {
        let file_name = &path[sep + 1..];
        if let Some(dot) = file_name.rfind('.') {
            Some(&file_name[..dot])
        } else {
            Some(file_name)
        }
    } else {
        Some("sdl player")
    }
}
